﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class bgCubeOne : baseCub, IPointerClickHandler, IPointerEnterHandler
{
	float size;
	Image img;
	Text posText;
	Sprite initSp;
	Color initColor;
	public Dictionary<int, toolOne> arrHasLevel = new Dictionary<int, toolOne>();
	public Vector2 dataPos;
	bool hasInit = false;
	public void init(Vector2 pos, string recycleType, Vector2 dataPos)
	{
		_init(pos, recycleType);
		if (!hasInit) {
			hasInit = true;
			size = GetComponent<RectTransform>().rect.width;
			img = GetComponentInChildren<Image>();
			initColor = img.color;
			posText = GetComponentInChildren<Text>();
			initSp = img.sprite;
		}
		this.dataPos = dataPos;
		setPosText();
		changeColor(false);
	}
	public void setPosText()
	{
		posText.text = $"({dataPos.x},{dataPos.y})";
	}
	public void changeColor(bool isWhit)
	{
		if (isWhit) {
			img.color = Color.white;
		} else {
			img.color = initColor;
		}
	}
	//清理提示
	public void onUnTip()
	{
		img.sprite = initSp;
		transform.localScale = initScale;
	}
	//提示
	public void onTip(string url)
	{
		img.sprite = Resources.Load<Sprite>("tool/" + url);
		transform.localScale = initScale * 1.2f;
	}
	//检测是否可以放下
	public bool checkPosIsOk(Vector2 pos, toolData data)
	{
		bool xIsOk = Mathf.Abs(transform.localPosition.x - pos.x) < 0.5f * size;
		bool yIsOk = Mathf.Abs(transform.localPosition.y - pos.y) < 0.5f * size;
		bool zIndexIsOk = !arrHasLevel.ContainsKey(data.zIndex);
		return xIsOk && yIsOk && zIndexIsOk;
	}
	//当放置方块
	public void onPlace(toolOne tool)
	{
		if (!arrHasLevel.ContainsKey(tool.data.zIndex)) {
			arrHasLevel.Add(tool.data.zIndex, tool);
		} else {
			//Debug.LogError ("重复放置" + tool.data.name);
		}
	}
	//当移掉方块
	public void onRemoveCube(toolData data)
	{
		if (arrHasLevel.ContainsKey(data.zIndex)) {
			arrHasLevel.Remove(data.zIndex);
		}
	}
	//点击复制最近点击的
	public void OnPointerClick(PointerEventData eventData)
	{
		if (Global.GameProxy.copyToolOne != null && !arrHasLevel.ContainsKey(Global.GameProxy.copyToolOne.data.zIndex)) {
			toolOne toolOne = Global.GameProxy.copyToolOne;
			toolData data = toolOne.data;
			GameObject cubeOne = Global.GameProxy.ControlBox.getPrefabByName(prefabType.toolOne);
			toolOne toolOneCom = cubeOne.GetComponent<toolOne>();
			Transform parent = Global.GameProxy.ControlBox.getAndCreatorLevel(data.zIndex);
			saveToolData saveTool = new saveToolData() {
				id = data.id,
				pos = dataPos,
				count = toolOne.saveData.count,
				dir = toolOne.saveData.dir,
			};
			toolOneCom.init(data.id, saveTool, parent, prefabType.toolOne, toolStatu.inPlan);
			toolOneCom.OnPointerClick(null);
		}
	}
	//左键点击复制最近点击，右键删除
	public void OnPointerEnter(PointerEventData eventData)
	{
		if (Input.GetMouseButton(0) && !Global.GameProxy.hasToolDraging) {
			OnPointerClick(eventData);
		}
	}
}